<template>
  <div class="product-add">
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane
        v-for="item in tabOptions"
        :key="item.id"
        :label="item.tradeType"
        :name="item.id"
      />
    </el-tabs>
    <!--form表单-->
    <el-form size="small" ref="form" :model="form" label-width="250px">
      <!--小程序设置-->
      <div class="common-form">购买须知</div>
      <el-form-item label="标题">
        <el-input v-model="form.title" class="max-w460"></el-input>
      </el-form-item>
      <el-form-item label="内容">
        <Uediter
          v-if="showEditor"
          :text="form.content"
          :config="ueditor.config"
          ref="ue"
          @contentChange="contentChangeFunc"
        ></Uediter>
      </el-form-item>

      <!--提交-->
      <div class="common-button-wrapper">
        <el-button type="primary" @click="onSubmit">提交</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
import PorductApi from "@/api/product.js";
import Uediter from "@/components/UE.vue";
// import AppSettingApi from "@/api/appsetting.js";

export default {
  data() {
    return {
      /*富文本框配置*/
      ueditor: {
        text: "",
        config: {
          initialFrameWidth: 600,
          initialFrameHeight: 600,
        },
      },

      /*切换菜单*/
      // activeIndex: '1',
      /*form表单数据*/
      form: {
        title: "",
        content: "",
      },

      tabOptions: [],
      activeName: 1,
      showEditor: false,
    };
  },
  components: {
    /*编辑器*/
    Uediter,
  },
  async created() {
    const res = await PorductApi.productByNoticeList();
    this.tabOptions = res.data || [];
    this.activeName = this.tabOptions[0].id;
    this.getData();
  },

  methods: {
    contentChangeFunc(e) {
      // console.log(e, "eeekmkmk");
      // this.content = e;
      this.form.content = e;
    },
    handleClick() {
      this.showEditor = false;
      this.$nextTick(() => {
        this.getData();
      });
    },
    getData() {
      let self = this;
      PorductApi.productByNoticeInfo({
        id: this.activeName,
      })
        .then((res) => {
          // console.log(res, "我是啥啊");
          if (res.data != null) {
            self.form = res.data;
            self.showEditor = true;
          }
        })
        .catch((error) => {});
    },
    //提交表单
    onSubmit() {
      console.log(this.form.content, "form.content");
      let self = this;
      let params = this.form;

      PorductApi.productByNoticeEdit(params, true)
        .then((data) => {
          ElMessage({
            message: "编辑成功",
            type: "success",
          });
          this.getData();
        })
        .catch((error) => {});
    },
  },
};
</script>

<style scoped>
.tips {
  color: #ccc;
}
</style>
